package com.ctrip.ec.api.admin.controller.forward;
import com.ctrip.ec.api.admin.common.ValidateException;
import com.ctrip.ec.api.admin.model.forward.ForwardActivityModel;
import com.ctrip.ec.api.admin.model.query.QueryModel;
import com.ctrip.ec.biz.dao.activity.ActivityDao;
import com.ctrip.ec.biz.dao.forward.ForwardGoodsDao;
import com.ctrip.ec.biz.entity.activity.ActivityEntity;
import com.ctrip.ec.biz.entity.forward.ForwardGoodsEntity;
import com.ctrip.ec.dal.PagerSetting;
import com.ctrip.ec.api.admin.common.RequestContext;
import com.ctrip.ec.biz.dao.forward.ForwardActivityDao;
import com.ctrip.ec.biz.entity.forward.ForwardActivityEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("forwardActivity")
public class ForwardActivityController{
    @Autowired
    private ForwardActivityDao forwardActivity;

    @Autowired
    private ForwardGoodsDao forwardGoods;



    @Autowired
    private ActivityDao activity;

    @PostMapping("list")
    public List<ActivityEntity> list(@RequestBody QueryModel model) throws  SQLException{
        PagerSetting setting = model.build();
        List<ForwardActivityEntity> list = forwardActivity.list(setting);
        RequestContext.setResponseHeaderCount(setting.getRecordCount());

        List<Long> activityIds = list.stream().map(t->t.getActivityId()).collect(Collectors.toList());
        if(activityIds.size()>0){
            List<ActivityEntity> activityList=activity.listByIds(activityIds);
            return activityList;
        }else{
            return new ArrayList<>();
        }
    }

    @GetMapping("detail/{id}")
    public  ForwardActivityModel detail(@PathVariable Long id) throws SQLException {
        //传过来的是activityId
        ForwardActivityEntity entity = forwardActivity.entityByActivityId(RequestContext.getAgentId(),id);

        ForwardActivityModel model = new ForwardActivityModel();
        model.copyFrom(entity);

        List<ForwardGoodsEntity> list = forwardGoods.list("forward_activity_id = " + model.getId());
        model.setGoodsList(list);

        return model;

    }

    @PostMapping("save")
    public  Long save(@RequestBody ForwardActivityModel entity) throws SQLException {
        ActivityEntity activityEntity = activity.entity(entity.getActivityId());
        if(activityEntity == null){
            throw new ValidateException("活动Id:" + entity.getActivityId() +"已不存在");
        }

        //判断此活动有没有被转发过来
        if(entity.add()){
            if(forwardActivity.existActivity(RequestContext.getAgentId(), entity.getActivityId())){
                throw new ValidateException("该活动已经被转发");
            }
        }

        if(entity.getGoodsList() == null || entity.getGoodsList().size()==0){
            throw  new ValidateException("请选择转发的商品");
        }

        entity.setAgentId(RequestContext.getAgentId());
        entity.setName(activityEntity.getName());
        entity.setBrandId(activityEntity.getBrandId());
        entity.setCreateTime(new Timestamp(System.currentTimeMillis()));
        forwardActivity.save(entity);

        //保存分享的商品
        //先删除原来的
        forwardGoods.deleteByForwardActivityId(entity.getId());
        for(ForwardGoodsEntity goodsEntity : entity.getGoodsList()){
            goodsEntity.setBrandId(entity.getBrandId());
            goodsEntity.setAgentId(entity.getAgentId());
            goodsEntity.setActivityId(entity.getActivityId());
            goodsEntity.setForwardActivityId(entity.getId());
            forwardGoods.save(goodsEntity);
        }


        return entity.getId();
    }

    @PostMapping("delete")
    public  void delete(@RequestBody List<Long> ids) throws SQLException {
        forwardActivity.delete(ids);
    }
    @PostMapping("erase")
    public void erase(@RequestBody List<Long> ids) throws  SQLException{
        forwardActivity.erase(ids);
    }

    @PostMapping("unerase")
    public  void unerase(@RequestBody List<Long> ids) throws  SQLException{
        forwardActivity.unerase(ids);
    }
}
